package com.chebaohui.iqs.sdk.chaojiying;

import java.util.HashMap;
import java.util.Map;

import com.alibaba.druid.support.logging.Log;
import com.alibaba.druid.support.logging.LogFactory;
import com.alibaba.fastjson.JSONObject;

/**
 * 超级鹰连接工具
 * 
 * @author xielonghai
 *
 */
public class ChaoJiYingSdk {
	//
	public static final String cjy_account = "REGASXIE";// 超级鹰账户
	public static final String cjy_password = "xlh900512";// 超级鹰账户密码
	public static final String cjy_softId = "893819";// 超级鹰软件ID

	/**
	 * logger:日志工具.
	 */
	private static Log log = LogFactory.getLog(ChaoJiYingSdk.class);

	private static final String value = "超级鹰验证码识别";// 日志输出描述

	/**
	 * @param img64
	 * @return
	 */
	public static Map<String, String> identifyBase64Img(String img64) {
		log.info("进入..." + value);
		Map<String, String> result = new HashMap<>();
		String img;
		try {
			img = java.net.URLEncoder.encode(img64, "UTF-8");// 转码
		} catch (Exception e) {
			result.put("state", "false");
			result.put("msg", e.getMessage());
			return result;
		}

		String identify = ChaoJiYing.PostPic_base64(cjy_account, cjy_password, cjy_softId, "1902", "0", img);
		log.info(value + "结果:" + identify);
		JSONObject json = JSONObject.parseObject(identify);
		if ("0".equals(json.getString("err_no"))) {
			String pic_str = json.getString("pic_str");
			if (null != pic_str && pic_str.trim().length() == 4) {
				result.put("state", "true");
				result.put("data", pic_str.trim());
				result.put("msg", "ok");
				result.put("pic_id", json.getString("pic_id"));//图片识别id
				return result;
			}
		}
		result.put("state", "false");
		result.put("msg", "超级鹰识别错误");

		return result;
	}

	/**
	 * 报错返分
	 * 
	 * @param picId
	 * @return
	 */
	public static String errorResponse(String picId) {
		return ChaoJiYing.ReportError(cjy_account, cjy_password, cjy_softId, picId);
	}
}
